#!/bin/bash

function jbase() {
    curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
    curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | tee /etc/apt/sources.list.d/caddy-stable.list
    add-apt-repository ppa:longsleep/golang-backports
    apt update -y
    apt upgrade -y
    
    # config ssh
    sed -i 's/Port/#&/g' /etc/ssh/sshd_config
    sed -i 's/PermitRootLogin/#&/g' /etc/ssh/sshd_config
    sed -i 's/ClientAliveInterval/#&/g' /etc/ssh/sshd_config
    sed -i 's/ClientAliveCountMax/#&/g' /etc/ssh/sshd_config
    sed -i 's/PasswordAuthentication/#&/g' /etc/ssh/sshd_config
    sed -i 's/Protocol/#&/g' /etc/ssh/sshd_config
    sed -i 's/PermitEmptyPasswords/#&/g' /etc/ssh/sshd_config
    sed -i 's/MaxAuthTries/#&/g' /etc/ssh/sshd_config
    sed -i 's/LoginGraceTime/#&/g' /etc/ssh/sshd_config
    sed -i 's/StrictModes/#&/g' /etc/ssh/sshd_config
    echo "Port 65432" >> /etc/ssh/sshd_config
    echo "StrictModes no" >> /etc/ssh/sshd_config
    echo "LoginGraceTime 60" >> /etc/ssh/sshd_config
    echo "MaxAuthTries 2" >> /etc/ssh/sshd_config
    echo "PermitRootLogin no" >> /etc/ssh/sshd_config
    echo "ClientAliveCountMax 3" >> /etc/ssh/sshd_config
    echo "ClientAliveInterval 60" >> /etc/ssh/sshd_config
    echo "PasswordAuthentication no" >> /etc/ssh/sshd_config
    echo "PermitEmptyPasswords no" >> /etc/ssh/sshd_config
    echo "Protocol 2" >> /etc/ssh/sshd_config
    useradd -m jone
    usermod -a -G wheel jone
    echo "jone  ALL=(ALL:ALL) NOPASSWD: ALL" >> /etc/sudoers
    usermod -L ubuntu
    mkdir /home/web_core
    chmod -R 777 /home/web_core
    chown jone:jone /home/web_core
    clear
    systemctl restart sshd
    systemctl restart ssh


    # caddy
    apt install -y debian-keyring debian-archive-keyring apt-transport-https
    apt install caddy


    systemctl enable caddy --now
    mkdir -p /etc/caddy/config
    sed -i 's/^/#/' /etc/caddy/Caddyfile
    echo "(redirect) {" >> /etc/caddy/Caddyfile
    echo "        @http {" >> /etc/caddy/Caddyfile
    echo "                protocol http" >> /etc/caddy/Caddyfile
    echo "        }" >> /etc/caddy/Caddyfile
    echo "        redir @http https://{host}{uri}" >> /etc/caddy/Caddyfile
    echo "}" >> /etc/caddy/Caddyfile
    echo "" >> /etc/caddy/Caddyfile
    echo "import ./config/*.ini" >> /etc/caddy/Caddyfile

    # supervisor
    apt install supervisor -y
    systemctl enable supervisor --now

    # mariadb
    apt install mariadb-server mariadb-client -y
    systemctl enable --now mariadb
    # mariadb utf8
    cp /etc/my.cnf /etc/my.cnf.bak
    echo "[client]" > /etc/my.cnf
    echo "default-character-set = utf8mb4" >> /etc/my.cnf
    echo "" >> /etc/my.cnf
    echo "[mysql]" >> /etc/my.cnf
    echo "default-character-set = utf8mb4" >> /etc/my.cnf
    echo "" >> /etc/my.cnf
    echo "[mysqld]" >> /etc/my.cnf
    echo "character-set-client-handshake = FALSE" >> /etc/my.cnf
    echo "character-set-server = utf8mb4" >> /etc/my.cnf
    echo "collation-server = utf8mb4_unicode_ci" >> /etc/my.cnf
    echo "" >> /etc/my.cnf
    cat /etc/my.cnf.bak >> /etc/my.cnf
    sed -i '/\[client\]/a\default-character-set=utf8mb4' /etc/my.cnf.d/client.cnf
    sed -i '/\[mysql\]/a\default-character-set=utf8mb4' /etc/my.cnf.d/mysql-clients.cnf
    # sed -i '/[server]/a\character-set-server=utf8mb4' /etc/my.cnf.d/server.cnf
    systemctl restart mariadb


    # golang

    apt install golang -y
    echo 'export GOPATH="${HOME}/go"' >> /root/.bashrc
    echo 'export GO111MODULE=on' >> /root/.bashrc
    echo "alias my='mycli -uroot '" >> /root/.bashrc
    echo 'export GOPATH="${HOME}/go"' >> /home/jone/.bashrc
    echo 'export GO111MODULE=on' >> /home/jone/.bashrc
    echo "alias my='mycli -uroot '" >> /home/jone/.bashrc
    # echo 'export GOPROXY=https://goproxy.cn' >> /root/.bashrc

    # mycli
    apt install -y mycli

    apt install libwebp-dev -y
    apt autoremove -y

    clear
    echo "set mysql with: mysql_secure_installation"
    echo "set ssh port to 65432 and reload ssh"
    echo "set password for user:jxxx"
}





function hlp(){
    clear
    echo ''
    echo 'jbase'
    echo -e '\tinstall caddy,maraidb,golang,supervisor,mycli and config ssh'
    echo 'jcaddy'
    echo -e '\tadd website'
    echo 'jsupervisor '
    echo -e "\tadd supervisor configure"
    echo ''
}

function jcaddy(){
    read -e -p "Input domain:" domain
    read -e -p "Input port:" port
    fname=${domain//./_}
    fname="${fname}.ini"
    echo "$domain {" >> /etc/caddy/config/$fname
    echo '    import redirect' >> /etc/caddy/config/$fname
    echo "    reverse_proxy  127.0.0.1:$port" >> /etc/caddy/config/$fname
    echo "}" >> /etc/caddy/config/$fname
    systemctl reload caddy
}

jsupervisor ()
{
    read -e -p "Input cmd path:" path
    read -e -p "Input cmd:" cmd
    path=${path%*/}
    fname=${cmd//./_}
    fname="${fname}.ini"
    echo "[program:$cmd]" >> /etc/supervisord.d/$fname
    echo "directory=$path" >> /etc/supervisord.d/$fname
    echo "command=$path/$cmd" >> /etc/supervisord.d/$fname
    echo "autostart=true" >> /etc/supervisord.d/$fname
    echo "autorestart=true" >> /etc/supervisord.d/$fname
    echo "user=root" >> /etc/supervisord.d/$fname
    systemctl restart supervisord
}

    if [[ $# < 1 ]]; then
        hlp
    else
        $@
    fi
